package io.itit.ecp.admin.server.service.impl;

import com.alibaba.fastjson.JSON;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import io.itit.ecp.admin.server.entity.SceneEdificeEntity;
import io.itit.ecp.admin.server.entity.SceneFloorEntity;
import io.itit.ecp.admin.server.entity.SceneRoomEntity;
import io.itit.ecp.admin.server.mapper.SceneFloorMapper;
import io.itit.ecp.admin.server.service.ISceneFloorService;
import io.itit.ecp.admin.server.service.ISceneRoomService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：楼层服务实现类
 * @Date：Create by 2023-11-23 09:55:22
 */
@Service
public class SceneFloorServiceImpl extends JoinServiceImpl<SceneFloorMapper, SceneFloorEntity> implements ISceneFloorService {

    @Resource
    private ISceneRoomService sceneRoomService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveEdificeFloors(String edificeId, String sceneProjectId, String floorJson) {
        boolean res = true;
        List<SceneFloorEntity> floors = JSON.parseArray(floorJson, SceneFloorEntity.class);
        List<Integer> nums = floors.stream().map(SceneFloorEntity::getFloorNum).collect(Collectors.toList());
        // 过滤并删除不要的楼层
        List<SceneFloorEntity> oldFloors = this.lambdaQuery()
                .eq(SceneFloorEntity::getSceneEdificeId, edificeId)
                .list();
        List<String> ids = oldFloors.stream().filter(sceneFloorEntity -> !nums.contains(sceneFloorEntity.getFloorNum())).map(SceneFloorEntity::getId).collect(Collectors.toList());
        if (!ids.isEmpty()) {
            res = this.removeByIds(ids);
        }

        // 保存/更新楼层信息
        List<SceneFloorEntity> updateEntitys = new ArrayList<>();
        List<SceneFloorEntity> saveEntitys = new ArrayList<>();
        floors.forEach(sceneFloorEntity -> {
            sceneFloorEntity.setSceneProjectId(sceneProjectId);
            if (sceneFloorEntity.getId() != null) {
                updateEntitys.add(sceneFloorEntity);
            } else {
                sceneFloorEntity.setSceneEdificeId(edificeId);
                saveEntitys.add(sceneFloorEntity);
            }
        });
        // 新增的
        res = res && (saveEntitys.size() <= 0 || saveBatch(saveEntitys));
        // 修改的
        res = res && (updateEntitys.size() <= 0 || updateBatchById(updateEntitys));
        return res;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean initFloorByEdifice(SceneEdificeEntity edifice) {
        List<SceneFloorEntity> floors = new ArrayList<>();
        for (int i = edifice.getStartNum(); i <= edifice.getEndNum(); i++) {
            if (i == 0) {
                continue;
            }
            SceneFloorEntity floor = new SceneFloorEntity();
            floor.setSceneProjectId(edifice.getSceneProjectId());
            floor.setSceneEdificeId(edifice.getId());
            floor.setFloorName(i + "层");
            floor.setFloorNum(i);
            floors.add(floor);
        }
        return this.saveBatch(floors);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeFloors(List<String> ids) {
        // 删除空间
        List<SceneRoomEntity> rooms = sceneRoomService.lambdaQuery()
                .select(SceneRoomEntity::getId)
                .in(SceneRoomEntity::getSceneFloorId, ids)
                .list();
        if (!rooms.isEmpty()) {
            List<String> floorIds = rooms.stream().map(SceneRoomEntity::getId).collect(Collectors.toList());
            sceneRoomService.removeRooms(floorIds);
        }
        // 删除楼层
        return this.removeBatchByIds(ids, 1000);
    }
}
